// pages/renew/renew.ts
import { Request } from '../../utils/util'
import Notify from '@vant/weapp/notify/notify';
const app = getApp()
Page({
    data: {
        endDay: "",
        endTime: '23:59',
        borrowInfo: {},
        showEndCalendar: false,
        showEndTimePicker: false,
        currentTime: '',
        loading:true
    },
    onLoad(option: any) {
        this.setData({id:option.id})
        Request({
            url: `/getborrowById?id=${option.id}`,
        }).then(res => {
            console.log(res)
            this.setData({
                borrowInfo: res,
            })
        }).catch(error => {
            console.log(error);
        });
        let date = new Date()
        let curDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
        this.setData({
            endDay: curDate,
        })
    },
    onReady(){
        this.setData({loading:false})
    },
    chooseDateEnd() {
        console.log("choseDare")
        this.setData({
            showEndCalendar: true,
        })
    },
    onCloseEnd() {
        this.setData({ showEndCalendar: false });
    },
    formatDate(date) {
        date = new Date(date);
        return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
    },
    onConfirmEnd(event) {
        this.setData({
            showEndCalendar: false,
            endDay: this.formatDate(event.detail),
        });
    },

    chooseTimeEnd() {
        this.setData({ showEndTimePicker: true })
    },
    confirmEndTime(e) {
        this.setData({ endTime: e.detail })
    },
    cancelEndTime(e) {
        this.setData({ showEndTimePicker: false })
    },
    onClickHideEnd() {
        this.setData({ showEndTimePicker: false })
    },
    submitBorrow(){
        let _this=this
        let renewDay=new Date(_this.data.endDay+' '+_this.data.endTime)
        console.log(_this.data.endTime)
        console.log("renewDay",renewDay)
        wx.showModal({
            title: '提示',
            content: `确认续借至${_this.data.endDay}`,
            success(res) {
                if (res.confirm) {
                    Request({
                        url: '/renewborrow',
                        method:'post',
                        data:{
                            data:{
                                renewDay:renewDay,
                                id:_this.data.id,
                            }
                        }
                    }).then(res => {
                        if(res.success==true){
                            Notify({
                                color: '#ffffff',
                                background: app.globalData.notifyColor,
                                message: '续借成功',
                                top: app.globalData.navBarHeight
                            });
                            setTimeout(function () {
                                wx.navigateBack()
                            }, 2000)
                        }else{
                            Notify({
                                color: '#ffffff',
                                background: app.globalData.notifyColor,
                                message: '借用失败',
                                top: app.globalData.navBarHeight
                            });
                        }
            
                    }).catch(error => {
                        console.log(error);
                    });
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
 
    }

})